package com.vcolco.utils;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * Title: redis工具类
 * Description:
 * Company: vcolco
 *
 * @author gujun
 * @date
 */
@Component("redisUtils")
public class RedisUtils {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private ValueOperations<String, String> valueOperations;
    @Autowired
    private HashOperations<String, String, Object> hashOperations;
    @Autowired
    private ListOperations<String, Object> listOperations;
    @Autowired
    private SetOperations<String, Object> setOperations;
    @Autowired
    private ZSetOperations<String, Object> zSetOperations;


    /**
     * 默认过期时长 1天，单位：秒
     */
    public final static long DEFAULT_EXPIRE = 60 * 60 * 24;
    /**
     * 不设置过期时长
     */
    public final static long NOT_EXPIRE = -1;

    public void set(String key, Object value, long expire) {
        valueOperations.set(key, toJson(value));
        if (expire != NOT_EXPIRE) {
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
    }

    public void set(String key, Object value) {
        set(key, value, DEFAULT_EXPIRE);
    }

    public <T> T get(String key, Class<T> clazz, long expire) {
        String value = valueOperations.get(key);
        if (expire != NOT_EXPIRE) {
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value == null ? null : fromJson(value, clazz);
    }

    public <T> T get(String key, Class<T> clazz) {
        return get(key, clazz, NOT_EXPIRE);
    }

    public String get(String key, long expire) {
        String value = valueOperations.get(key);
        if (expire != NOT_EXPIRE) {
            redisTemplate.expire(key, expire, TimeUnit.SECONDS);
        }
        return value;
    }

    public String get(String key) {
        return get(key, NOT_EXPIRE);
    }

    public boolean delete(String key) {
        return redisTemplate.delete(key);
    }

    /**
     * Object转成JSON数据
     */
    private String toJson(Object object) {
        if (object instanceof Integer || object instanceof Long || object instanceof Float ||
                object instanceof Double || object instanceof Boolean || object instanceof String) {
            return String.valueOf(object);
        }
        return JSON.toJSONString(object);
    }

    /**
     * JSON数据，转成Object
     */
    private <T> T fromJson(String json, Class<T> clazz) {
        return JSON.parseObject(json,clazz);
    }

    /**
     * hashmap 设置值
     * @param key redis中的key
     * @param hkey 对应的子key
     * @param value 值
     * @param isOverdue 是否设置当晚过期
     */
    public void hset(String key,String hkey,Object value,boolean isOverdue){
        hashOperations.put(key,hkey,value);
    }

    /**
     * hashmap 获取值
     * @param key
     * @param hkey
     * @return
     */
    public String hget(String key,String hkey){
        Object ob =  hashOperations.get(key,hkey);
        return toJson(ob);
    }

    /**
     * 判断某个值是否存在
     * @param key
     * @return
     */
    public Boolean hasKey(String key){
        return redisTemplate.hasKey(key);
    }

    /**
     * 获取某个key的所有key和值
     * @param key
     * @return
     */
    public Map<String, Object> hget(String key){
        return hashOperations.entries(key);
    }

    /**
     * 数字自增
     * @param key
     * @return
     */
    public long incr(String key){
        Long increment = redisTemplate.opsForValue().increment(key, 1);
        return increment;
    }

    /**
     * Title: list集合rpush 右压入
     * Description:
     * Company: vcolco
     *
     * @author gujun
     * @date 2019年7月23日10:54:57
     */
    public boolean rpush(String key,Object value){
        Long inserNum = redisTemplate.opsForList().rightPush(key, value);
        if (inserNum > 0){
            return true;
        }
        return false;
    }

    /**
     * Title: list集合rpush 右压入
     * Description:
     * Company: vcolco
     *
     * @author gujun
     * @date 2019年7月23日10:54:57
     */
    public Long rpushAll(String key, Collection<Object> values){
        return listOperations.rightPushAll(key, values);
    }

    /**
     * 左弹出
     */
    public Object lpop(String key){
        return redisTemplate.opsForList().leftPop(key);
    }

    /**
     * 设置过期时间
     */
    public boolean expire(String key,Long expire){
        return redisTemplate.expire(key, expire, TimeUnit.SECONDS);
    }

    /**
     * setnx
     * @param key
     * @param value
     * @param overTime
     * @return
     */
    public boolean setnx(String key,Object value,Long overTime){
        return redisTemplate.opsForValue().setIfAbsent(key,value,overTime,TimeUnit.MILLISECONDS);
    }

    /**
     * 模糊查询key 查询结果为key*
     * @param key
     * @return
     */
    public Set<String> keys(String key){
        return redisTemplate.keys(key + "*");
    }

    /**
     * 一次性获取多个数据
     * @param names
     * @return
     */
    public Map<String,Object> mget(List<String> names){
        List<Object> objects = redisTemplate.opsForValue().multiGet(names);
        Map<String,Object> map = new HashMap<>();
        for (int i = 0; i < names.size() ; i++){
            map.put(names.get(i),objects.get(i));
        }
        return map;
    }


}
